<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Songs extends CI_Controller {

    function __construct() {
        parent::__construct();
         if ($this->session->userdata('login') == FALSE) {
            redirect('login');
        }
    }

    function index() {
       
        $this->load->model('menu_model'); 
        $this->load->model('songs_model');
        
        $this->load->library('pagination');
        
        $page=$this->uri->segment(3);
        $limit=24;
         if(!$page):
            $offset = 0;
        else:
            $offset = $page;
        endif; 
         
        $songs = $this->songs_model->_loadPage($offset, $limit);
        $songsCount = $this->songs_model->_loadPageCount();
        
        $config = array();
        $config['base_url'] = base_url() . '/songs/index/';
        $config['total_rows'] = $songsCount['jml'];
        $config['per_page'] = $limit;
        $config['uri_segment'] = 3;
        $config['first_link'] = 'First';
        $config['last_link'] = 'Last';
        $config['next_link'] = 'Next';
        $config['prev_link'] = 'Prev';
        $config['num_tag_open'] = '<li>';
        $config['num_tag_close'] = '</li>';
        $config['prev_tag_open'] = '<li>';
        $config['prev_tag_close'] = '</li>';
        $config['next_tag_open'] = '<li>';
        $config['next_tag_close'] = '</li>';
        $config['last_tag_open'] = '<li>';
        $config['last_tag_close'] = '</li>';
        $config['first_tag_open'] = '<li>';
        $config['first_tag_close'] = '</li>';
        $config['cur_tag_open'] = '<li><a href="#" class="active"><b>';
        $config['cur_tag_close'] = '</b></a></li>';
        $this->pagination->initialize($config);
        
        $data = array();
        $data['current'] = 'Songs';
        $data['link'] = 'songs';
        $data['menus'] = $this->menu_model->_load();
        $data['songs'] = $songs;
        
        $this->load->view('songs-view',$data);
    }
    
    function add() {
        $this->load->model('menu_model');   
        $this->load->model('songs_model');   
        
        $data = array();
        $data['current'] = 'Gallery';
        $data['link'] = 'songs';
        $data['menus'] = $this->menu_model->_load();
        $data['genres'] = $this->songs_model->_loadGenre();
        $data['countries'] = $this->songs_model->_loadCountry();
        
        $this->load->view('songs-add-view',$data);        
    }
    
    function view($id) {
        $this->load->model('songs_model');
        
        $data = array();
        $data['songs'] = $this->songs_model->_getById($id);
        $data['audio'] = $this->songs_model->_getAudio($id);
        $data['video'] = $this->songs_model->_getVideo($id);
        $data['rtsp'] = $this->songs_model->_getRTSP($id);
        $data['genres'] = $this->songs_model->_loadGenre();
        $data['countries'] = $this->songs_model->_loadCountry();
		 
		
        $this->load->view('songs-detail-view',$data);        
    }
      
    function processInsert() {
        
        $this->load->model('songs_model');
        
        $data = array();
        $data['kode'] = $this->input->post('kode',true);
        $data['title'] = $this->input->post('title',true);  
        $data['artist'] = $this->input->post('artist',true);  
        $data['genre'] = $this->input->post('genre',true);  
        $data['country'] = $this->input->post('country',true);  
        $data['album'] = $this->input->post('album',true);  
        $data['insert_date'] = date("Y-m-d H:i:s");
        
        $file_name = $data['kode'].'-'.$data['artist'].'-'.$data['title'].'-'.date("s").''.rand(1,9999);
        $file_name = str_replace(' ', '_', $file_name);
        $file_name = preg_replace('/[^A-Za-z0-9\-]/', '', $file_name);
                
        $data['path_thumbnail'] = $this->songs_model->uploadImage($file_name);        
        $this->db->insert('m_song',$data);
        
        $id_song = $this->songs_model->_getMaxId();
        
        $data_audio = array();        
        $data_audio['type'] = 'audio';
        $data_audio['id_song'] = $id_song;
        $data_audio['path_file'] = $this->songs_model->uploadAudio($file_name);
        $this->db->insert('m_song_file',$data_audio);
        
        $data_video = array();        
        $data_video['type'] = 'video';
        $data_video['id_song'] = $id_song;
        $data_video['path_file'] = $this->songs_model->uploadVideo($file_name);
        $this->db->insert('m_song_file',$data_video);
        
         redirect('songs');
    }
      
    function processUpdate($id_song) {
    
         $this->load->model('songs_model');
        
        $data = array();
        $data['kode'] = $this->input->post('kode',true);
        $data['title'] = $this->input->post('title',true);  
        $data['artist'] = $this->input->post('artist',true);  
        $data['genre'] = $this->input->post('genre',true);  
        $data['country'] = $this->input->post('country',true);
        $data['album'] = $this->input->post('album',true);  
        $data['insert_date'] = date("Y-m-d H:i:s");
        
        $file_name = $data['kode'].'-'.$data['artist'].'-'.$data['title'].'-'.date("s").''.rand(1,9999);
        $file_name = str_replace(' ', '_', $file_name);
        $file_name = preg_replace('/[^A-Za-z0-9\-]/', '', $file_name);
        
        $path_thumbnail = $_FILES['path_thumbnail']['name'];
        $path_thumbnail_audio = $_FILES['path_file_audio']['name'];
        $path_thumbnail_video = $_FILES['path_file_video']['name'];
        
        if(!empty($path_thumbnail)) {
        $data['path_thumbnail'] = $this->songs_model->uploadImage($file_name);        
        }
        
        $this->db->update('m_song',$data,'id = '.$id_song);
        
        
        if(!empty($path_thumbnail_audio)) {
        $data_audio = array();        
        $data_audio['id_song'] = $id_song;
        $data_audio['path_file'] = $this->songs_model->uploadAudio($file_name);
        $this->db->update('m_song_file',$data_audio,'id_song = '.$id_song.' AND type = audio'); 
        }
        
        if(!empty($path_thumbnail_video)) {
        $data_video = array();        
        $data_video['id_song'] = $id_song;
        $data_video['path_file'] = $this->songs_model->uploadVideo($file_name);
        $this->db->update('m_song_file',$data_video,'id_song = '.$id_song.'AND type = video');
        }
        
         redirect('songs');
    }
    
    function processDelete($id) {
        $this->db->delete('m_song',"id = $id");
         redirect('songs');
    }
    
      
}
?>